home *** CD-ROM | disk | FTP | other *** search
- if(!com) var com={};
- if(!com.VidBar) com.VidBar={};
-
- com.VidBar.DownloadQueue = function() {
- }
-
- com.VidBar.DownloadQueue.prototype = {
- downloader : null,
- downloadQueue : null,
- db:null,
- onLoad : function(event) {
- this.db = new com.VidBar.VidDB();
- this.dlMgr = Components.classes["@mozilla.org/download-manager;1"]
- .getService(Components.interfaces.nsIDownloadManager);
-
- this.update();
- this.syncDownloads();
-
- var _this = this;
- this.updateTimer = setInterval(function() {
- _this.update();
- }, 1000);
- this.syncTimer = setInterval(function() {
- _this.syncDownloads();
- }, 3000);
- },
- onUnload : function(event) {
- if (this.updateTimer) {
- clearInterval(this.updateTimer);
- }
- if (this.syncTimer) {
- clearInterval(this.syncTimer);
- }
- },
- syncDownloads : function(){
- this.db.syncDownloads(this.dlMgr);
- },
- update : function() {
- this.updateWaitQueue();
- this.updateDownloadList();
- },
- updateWaitQueue : function() {
- var children = document.getElementById("queueChildren");
- var queue = this.db.getWaitings();
- var items = [];
- for (var i = 0; i < queue.length; i++) {
- var entry = queue[i];
- var item = document.getElementById("queue-" + entry.guid);
- var filenameCell, sizeCell;
- if (item) {
- // found, update
- var filenameCell = item.childNodes[0].childNodes[0];
- sizeCell = filenameCell.nextSibling;
- } else {
- // not found, create
- item = document.createElement("treeitem");
- item.setAttribute("id", "queue-" + entry.guid);
- var row = document.createElement("treerow");
- var filenameCell = document.createElement("treecell");
- row.appendChild(filenameCell);
- sizeCell = document.createElement("treecell");
- row.appendChild(sizeCell);
- item.appendChild(row);
- children.appendChild(item);
- }
- filenameCell.setAttribute("label", entry.filename);
- sizeCell.setAttribute("label", VidUtils.getSizeStr(entry.size));
- items.push(item);
- }
- var removeList = [];
- for (var i = 0; i < children.childNodes.length; i++) {
- var found = false;
- for (var j = 0; j < items.length; j++) {
- if (children.childNodes[i] == items[j]) {
- found = true;
- break;
- }
- }
- if (!found) {
- removeList.push(children.childNodes[i]);
- }
- }
- for (var i = 0; i < removeList.length; i++) {
- children.removeChild(removeList[i]);
- }
- },
- updateDownloadList : function() {
- var children = document.getElementById("downChildren");
- var list = this.db.getDownloadings();
- var items = [];
- for (var i = 0; i < list.length; i++) {
- var entry = list[i];
- var item = document.getElementById("down-" + entry.guid);
- var filenameCell, statusCell, sizeCell, percentCell;
- if (item) {
- // found, update
- var filenameCell = item.childNodes[0].childNodes[0];
- statusCell = filenameCell.nextSibling;
- sizeCell = statusCell.nextSibling;
- percentCell = sizeCell.nextSibling;
- } else {
- // not found, create
- item = document.createElement("treeitem");
- item.setAttribute("id", "down-" + entry.guid);
- var row = document.createElement("treerow");
- filenameCell = document.createElement("treecell");
- row.appendChild(filenameCell);
- statusCell = document.createElement("treecell");
- row.appendChild(statusCell);
- sizeCell = document.createElement("treecell");
- row.appendChild(sizeCell);
- percentCell = document.createElement("treecell");
- row.appendChild(percentCell);
- item.appendChild(row);
- children.appendChild(item);
- }
- filenameCell.setAttribute("label", entry.filename);
- var statusDisplay = VidUtils.getDownloadStatusDisplay(entry.status);
- statusCell.setAttribute("label", statusDisplay[0]);
- sizeCell.setAttribute("label", VidUtils.getSizeStr(entry.size));
- percentCell.setAttribute("label", (entry.percentComplete||"0") + "%");
- items.push(item);
- }
- var removeList = [];
- for (var i = 0; i < children.childNodes.length; i++) {
- var found = false;
- for (var j = 0; j < items.length; j++) {
- if (children.childNodes[i] == items[j]) {
- found = true;
- break;
- }
- }
- if (!found) {
- removeList.push(children.childNodes[i]);
- }
- }
- for (var i = 0; i < removeList.length; i++) {
- children.removeChild(removeList[i]);
- }
- },
- onSelect:function(){
- var enableRemove = false;
- var tree = document.getElementById("vidbar-wait-queue");
- if(tree.view==null)
- enableRemove = false;
- else if(tree.view.selection.count>0)
- enableRemove = true;
- document.getElementById("vidbar-queue-remove").disabled = !enableRemove;
- },
- removeFromQueue : function() {
- var tree = document.getElementById("vidbar-wait-queue");
- if (tree.view == null)
- return;
- else if (tree.view.selection.count == 0)
- return;
-
- var sel = [];
- var numRanges = tree.view.selection.getRangeCount();
- for (var t = 0; t < numRanges; t++) {
- var start = {};
- var end = {};
- tree.view.selection.getRangeAt(t, start, end);
- for (var v = start.value; v <= end.value; v++) {
- if (v >= 0) {
- var item = tree.view.getItemAtIndex(v);
- var id = item.getAttribute("id");
- if(id.substr(0, 6)=="queue-"){
- sel.push(id.substr(6));
- }
- }
- }
- }
- com.VidBar.__d("removeFromQueue: selected items: "+sel);
- this.db.removeWaitings(sel);
- },
- cleanQueue:function(){
- this.db.removeAllWaitings();
- }
- };
-